<template>
  <div class="supplier-management">
    <!-- 操作栏 -->
    <div class="operation-bar">
      <el-input
        v-model="searchText"
        placeholder="搜索供应商ID/名称/品类"
        style="width: 360px"
        clearable
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>新增供应商
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table 
      :data="filteredSuppliers" 
      border 
      style="width: 100%"
      header-row-class-name="table-header"
      stripe
      highlight-current-row
    >
      <el-table-column prop="supplierId" label="供应商ID" width="200" sortable align="center" />
      <el-table-column prop="supplierName" label="供应商名称" width="220" />
      <el-table-column prop="contact" label="联系人" width="230">
        <template #default="{ row }">
          <el-tag effect="plain" type="info">
            {{ row.contact }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="supplyCategory" label="供应品类" width="190">
        <template #default="{ row }">
          <el-tag effect="plain" :type="categoryTagType[row.supplyCategory]">
            {{ row.supplyCategory }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="coopStatus" label="合作状态" width="160">
        <template #default="{ row }">
          <el-tag :type="statusTagType[row.coopStatus]" effect="light">
            {{ statusOptions.find(s => s.value === row.coopStatus)?.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="license" label="营业执照号" width="240" />
      <el-table-column prop="coopDate" label="合作日期" width="160" sortable />
      <el-table-column label="操作" width="200" fixed="right" align="center">
        <template #default="{ row }">
          <el-button size="small" type="primary" plain @click="handleEdit(row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            plain
            @click="handleDelete(row.supplierId)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEditMode ? '编辑供应商信息' : '新增供应商'"
      width="680"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="供应商ID" prop="supplierId">
          <el-input 
            v-model="formData.supplierId" 
            :disabled="isEditMode"
            placeholder="MV开头+7位数字"
          />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="formData.supplierName" placeholder="请输入供应商全称"/>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="formData.contact" placeholder="负责人姓名及电话"/>
        </el-form-item>
        <el-form-item label="供应品类" prop="supplyCategory">
          <el-select v-model="formData.supplyCategory" placeholder="请选择品类">
            <el-option
              v-for="item in categories"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="合作状态" prop="coopStatus">
          <el-select v-model="formData.coopStatus" placeholder="请选择状态">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="营业执照号" prop="license">
          <el-input v-model="formData.license" />
        </el-form-item>
        <el-form-item label="合作日期" prop="coopDate">
          <el-date-picker
            v-model="formData.coopDate"
            type="date"
            placeholder="选择日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 供应品类配置（参考网页2影视行业分类）
const categories = [
  '影视设备租赁',
  '特效制作',
  '版权采购',
  '影院耗材',
  '衍生品生产',
  '宣传发行'
]

// 合作状态选项（参考网页7供应商分级）
const statusOptions = [
  { value: '战略', label: '战略合作' },
  { value: '优先', label: '优先合作' },
  { value: '观察', label: '观察期' },
  { value: '冻结', label: '冻结合作' }
]

// 标签样式映射（影院主题色）
const categoryTagType = {
  '影视设备租赁': 'info',
  '特效制作': 'warning',
  '版权采购': 'success',
  '影院耗材': 'danger',
  '衍生品生产': 'primary',
  '宣传发行': ''
}

const statusTagType = {
  '战略': 'success',
  '优先': 'warning',
  '观察': 'danger',
  '冻结': ''
}

// 初始供应商数据（10条影视行业示例）
const suppliers = ref([
  {
    supplierId: 'MV2024001',
    supplierName: '光影设备租赁集团',
    contact: '张制片 13800001111',
    supplyCategory: '影视设备租赁',
    coopStatus: '战略',
    license: '91370200MA3F6B3K1C',
    coopDate: '2020-03-15'
  },
  {
    supplierId: 'MV2024002',
    supplierName: '幻影数字特效',
    contact: '王特效总监 13922223333',
    supplyCategory: '特效制作',
    coopStatus: '优先',
    license: '91370200MA3F4W8J8Y',
    coopDate: '2021-06-20'
  },
  {
    supplierId: 'MV2024003',
    supplierName: '银河版权代理',
    contact: '李版权经理 18612345678',
    supplyCategory: '版权采购',
    coopStatus: '战略',
    license: '91370200MA3F3R7J7X',
    coopDate: '2019-09-10'
  },
  {
    supplierId: 'MV2024004',
    supplierName: '星幕耗材供应',
    contact: '陈采购主任 15987654321',
    supplyCategory: '影院耗材',
    coopStatus: '观察',
    license: '91370200MA3F2T6K6W',
    coopDate: '2022-08-01'
  },
  {
    supplierId: 'MV2024005',
    supplierName: '漫威衍生品工厂',
    contact: '赵生产主管 13700008888',
    supplyCategory: '衍生品生产',
    coopStatus: '冻结',
    license: '91370200MA3F1Q5J5V',
    coopDate: '2018-04-12'
  },
  {
    supplierId: 'MV2024006',
    supplierName: '极光传媒发行',
    contact: '周发行总监 18812341234',
    supplyCategory: '宣传发行',
    coopStatus: '战略',
    license: '91370200MA3F0P4J4U',
    coopDate: '2023-11-05'
  },
  {
    supplierId: 'MV2024007',
    supplierName: 'IMAX设备中国',
    contact: '钱技术总监 13566778899',
    supplyCategory: '影视设备租赁',
    coopStatus: '优先',
    license: '91370200MA3FZ94J3T',
    coopDate: '2022-02-28'
  },
  {
    supplierId: 'MV2024008',
    supplierName: '天工异彩特效',
    contact: '孙视效总监 18609870987',
    supplyCategory: '特效制作',
    coopStatus: '战略',
    license: '91370200MA3FY83J2S',
    coopDate: '2020-07-19'
  },
  {
    supplierId: 'MV2024009',
    supplierName: '杜比音响中国',
    contact: '郑技术经理 13900998877',
    supplyCategory: '影院耗材',
    coopStatus: '观察',
    license: '91370200MA3FX72J1R',
    coopDate: '2021-03-10'
  },
  {
    supplierId: 'MV2024010',
    supplierName: '华纳兄弟发行',
    contact: '吴国际发行 15843214321',
    supplyCategory: '宣传发行',
    coopStatus: '战略',
    license: '91370200MA3FW61J0Q',
    coopDate: '2019-09-22'
  },
  {
    supplierId: 'MV2024004',
    supplierName: '星幕耗材供应',
    contact: '陈采购主任 15987654321',
    supplyCategory: '影院耗材',
    coopStatus: '观察',
    license: '91370200MA3F2T6K6W',
    coopDate: '2022-08-01'
  },
  {
    supplierId: 'MV2024005',
    supplierName: '漫威衍生品工厂',
    contact: '赵生产主管 13700008888',
    supplyCategory: '衍生品生产',
    coopStatus: '冻结',
    license: '91370200MA3F1Q5J5V',
    coopDate: '2018-04-12'
  },
  {
    supplierId: 'MV2024006',
    supplierName: '极光传媒发行',
    contact: '周发行总监 18812341234',
    supplyCategory: '宣传发行',
    coopStatus: '战略',
    license: '91370200MA3F0P4J4U',
    coopDate: '2023-11-05'
  },
  {
    supplierId: 'MV2024007',
    supplierName: 'IMAX设备中国',
    contact: '钱技术总监 13566778899',
    supplyCategory: '影视设备租赁',
    coopStatus: '优先',
    license: '91370200MA3FZ94J3T',
    coopDate: '2022-02-28'
  },
  {
    supplierId: 'MV2024008',
    supplierName: '天工异彩特效',
    contact: '孙视效总监 18609870987',
    supplyCategory: '特效制作',
    coopStatus: '战略',
    license: '91370200MA3FY83J2S',
    coopDate: '2020-07-19'
  },
  {
    supplierId: 'MV2024006',
    supplierName: '极光传媒发行',
    contact: '周发行总监 18812341234',
    supplyCategory: '宣传发行',
    coopStatus: '战略',
    license: '91370200MA3F0P4J4U',
    coopDate: '2023-11-05'
  },
  {
    supplierId: 'MV2024007',
    supplierName: 'IMAX设备中国',
    contact: '钱技术总监 13566778899',
    supplyCategory: '影视设备租赁',
    coopStatus: '优先',
    license: '91370200MA3FZ94J3T',
    coopDate: '2022-02-28'
  },
  {
    supplierId: 'MV2024008',
    supplierName: '天工异彩特效',
    contact: '孙视效总监 18609870987',
    supplyCategory: '特效制作',
    coopStatus: '战略',
    license: '91370200MA3FY83J2S',
    coopDate: '2020-07-19'
  }
])

// 搜索功能
const searchText = ref('')
const filteredSuppliers = computed(() => {
  return suppliers.value.filter(supplier => {
    return Object.values(supplier).some(value => 
      String(value).toLowerCase().includes(searchText.value.toLowerCase())
  )})
})

// 对话框相关状态
const dialogVisible = ref(false)
const isEditMode = ref(false)
const formRef = ref(null)
const formData = reactive({
  supplierId: '',
  supplierName: '',
  contact: '',
  supplyCategory: '',
  coopStatus: '战略',
  license: '',
  coopDate: new Date().toISOString().split('T')[0]
})

// 表单验证规则（参考网页6数据库设计）
const formRules = {
  supplierId: [
    { required: true, message: '请输入供应商ID', trigger: 'blur' },
    { pattern: /^MV\d{7}$/, message: '格式要求：MV+7位数字', trigger: 'blur' }
  ],
  supplierName: [{ required: true, message: '请输入供应商名称', trigger: 'blur' }],
  supplyCategory: { required: true, message: '请选择供应品类', trigger: 'change' },
  license: [
    { required: true, message: '请输入营业执照号', trigger: 'blur' },
    { pattern: /^[A-Z0-9]{18}$/, message: '请输入有效营业执照号', trigger: 'blur' }
  ],
  coopDate: [{ required: true, message: '请选择合作日期', trigger: 'change' }]
}

// 操作函数
const handleAdd = () => {
  isEditMode.value = false
  resetForm()
  dialogVisible.value = true
}

const handleEdit = (row) => {
  isEditMode.value = true
  Object.assign(formData, { ...row })
  dialogVisible.value = true
}

const handleDelete = (supplierId) => {
  ElMessageBox.confirm('确定要删除该供应商吗？', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    suppliers.value = suppliers.value.filter(u => u.supplierId !== supplierId)
    ElMessage.success('删除成功')
  })
}

const submitForm = async () => {
  await formRef.value.validate()
  if (isEditMode.value) {
    const index = suppliers.value.findIndex(u => u.supplierId === formData.supplierId)
    suppliers.value.splice(index, 1, { ...formData })
  } else {
    if (suppliers.value.some(u => u.supplierId === formData.supplierId)) {
      return ElMessage.warning('供应商ID已存在')
    }
    suppliers.value.push({ ...formData })
  }
  dialogVisible.value = false
  ElMessage.success(isEditMode.value ? '修改成功' : '新增成功')
}

const resetForm = () => {
  formData.supplierId = ''
  formData.supplierName = ''
  formData.contact = ''
  formData.supplyCategory = ''
  formData.coopStatus = '战略'
  formData.license = ''
  formData.coopDate = new Date().toISOString().split('T')[0]
}
</script>

<style scoped lang="scss">
.supplier-management {
  padding: 24px;
  background: #1a1a2e; /* 影院暗夜蓝 */
  min-height: 100vh;

  .operation-bar {
    padding: 16px 24px;
    background: rgba(40,40,60,0.9); /* 影院暗场灰 */
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(231,76,60,0.2); /* 影院红阴影 */
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    gap: 16px;
  }

  :deep(.table-header) th {
    background-color: #2c3e50 !important; /* 深蓝背景 */
    color: #e6e6e6;
    font-weight: 600;
    height: 56px;
  }

  .el-table {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(231,76,60,0.2); /* 影院红阴影 */
    background: rgba(40,40,60,0.95); /* 暗场背景 */
    
    .el-tag {
      font-size: 13px;
      padding: 0 8px;
      height: 26px;
      line-height: 24px;
    }
  }
}

.el-dialog {
  border-radius: 12px;
  background: #2c3e50; /* 深蓝主题 */
  
  .el-dialog__header {
    border-bottom: 1px solid #e74c3c; /* 影院红 */
    margin: 0 24px;
    padding: 24px 0 16px;
    color: #ffffff;
  }
  
  .el-dialog__body {
    padding: 24px 32px;
    color: #ecf0f1;
  }
  
  .el-dialog__footer {
    border-top: 1px solid #e74c3c; /* 影院红 */
    padding: 16px 24px;
  }
}

.el-form-item {
  margin-bottom: 20px;
  :deep(.el-form-item__label) {
    color: #ecf0f1;
  }
}
</style>